//
//  ProductListUIP.m
//  mbstore
//
//  Created by huangyi on 14-3-17.
//  Copyright (c) 2014年 huangyi. All rights reserved.
//

#import "ProductListUIP.h"
#import "SuitUIV.h"
#import "SuitProductListCell.h"

static UIPopoverController *popoverUI=nil;

@interface ProductListUIP ()

@end

@implementation ProductListUIP

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.pdts.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier=@"pdt";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.clipsToBounds=YES;
        cell.selectionStyle=UITableViewCellSelectionStyleGray;
        SuitProductListCell *content=(id)[NibLoader loadNib:@"SuitProductListCell"];
        [cell addSubview:content];
        content.tag=1001;
    }
    NSString *skuId=[self.pdts objectAtIndex:indexPath.row];
      #warning 调用数据库
    NSDictionary *item=[AppUI getProductDict:skuId];
    SuitProductListCell *content=(id)[cell viewWithTag:1001];
    [content setCellView:item];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *itemId=[self.pdts objectAtIndex:indexPath.row];
    if (popoverUI.isPopoverVisible) {
        [popoverUI dismissPopoverAnimated:NO];
        dispatch_async(dispatch_get_main_queue(), ^{
            SuitUIV *suit=self.delegate;
            [suit showProductItem:itemId];
        });
    }
}

-(void)showFromView:(UIView*)view{
    UIPopoverController *pop=[[UIPopoverController alloc] initWithContentViewController:self];
    [pop presentPopoverFromRect:view.frame inView:view.superview permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    pop.delegate=self;
    popoverUI=pop;
}
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
    popoverUI=nil;
}

-(void)setItems:(NSArray*)items{
    self.pdts=items;
    CGSize sz=CGSizeMake(300, 100*self.pdts.count);
//    self.contentSizeForViewInPopover=sz;
    self.preferredContentSize=sz;
}

@end
